<template>
	<view class="index">
		<view class="form-wrapper">
			<view class="item-wrapper">
				供应商：{{ form.supplierCode }}-{{form.supplierName}}
			</view>
		</view>
		<view class="form-wrapper">
			<view class="item-wrapper">
				<view class="label">
					城市仓：
				</view>
				<text>{{ form.cityCode }}{{ form.cityName }}.{{ form.pickupPointCode }}{{form.pickupPointName}}</text>
			</view>
			<view class="item-wrapper marginTop" v-for="(item,index) in form.vehicleList" :key="index">
				<view class="label">
					{{ index === 0 ? '停车位：':'' }}
				</view>
				<text>{{ item.parkingSpace }} {{ item.vehicleCode }}</text>
			</view>
		</view>
		<view v-if="form.confirmTime" class="form-wrapper tip-wrapper">
			<view class="tip">
				上次回单时间：{{ form.confirmTime }}
			</view>
			<view class="tip">
				回单信息如下，再次回单将覆盖上次回单实际交货件数
			</view>
		</view>
		<view class="product-item" v-for="(item,index) in form.productList" :key="index">
			<view class="product-name-wrapper">
				<view class="productName">
					{{ index + 1 }}、{{ item.productName }}
				</view>
				<uni-icons type="right" size="16" color="#999999"></uni-icons>
			</view>
			<view class="info-wrapper">
				<text>货号：{{ item.productCode }}</text>
				<text class="bold">客户订货：{{ item.quantity }}件</text>
			</view>
			<view class="info-wrapper">
				<text class="bold">实际交货：</text>
				<input type="number" v-model="item.confirmQuantity" placeholder="实际交货件数" class="input" />
				<text>件</text>
			</view>
		</view>
		<view class="images-wrapper" v-if="historyImageList.length">
			<view class="title-wrapper">
				<view class="title">
					历史回单小票
				</view>
			</view>
			<view class="product-image-list">
				<view class="item" v-for="(item,index) in historyImageList" :key="index">
					<image @click="openPic(index,0)" :src="item+ '?imageView2/3/w/300/q/100'" class="product-image"
						mode="aspectFill">
					</image>
				</view>
			</view>
		</view>
		<view class="images-wrapper">
			<view class="title-wrapper">
				<view class="title">
					回单拍照(拍照留存回单小票)
				</view>
			</view>
			<view class="product-image-list">
				<view class="item" v-for="(item,index) in imagelist" :key="index">
					<image @click="openPic(index,1)" :src="item+ '?imageView2/3/w/300/q/100'" class="product-image"
						mode="aspectFill">
					</image>
					<view class="del-wrapper" @click="delImage(index)">
						<uni-icons type="trash" color="#ffffff" size="20"></uni-icons>
					</view>
				</view>
				<view class="item add-item" @click="uploadByImage">
					<uni-icons type="plusempty" size="40" color="#A2A6A3"></uni-icons>
				</view>
			</view>
		</view>


		<view class="btn-wrapper">
			<view class="btn default" @click="goRecord">
				交货记录
			</view>
			<view class="btn" v-if="$permissions.verify('app:shipment:add')" @click="submit">
				确认交货回单
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {},
				imagelist: [],
				historyImageList: [],
				disabled: false
			}
		},
		onLoad(option) {
			this.getData(option.code)
		},
		methods: {
			async getData(id) {
				const data = await this.$api.post('/adm/supply/record/getReceiptRecord', {
					id: id
				})
				if (data.code === 200) {
					if (data.data.confirmImage) {
						this.historyImageList = data.data.confirmImage.split(',')
					}
					this.form = data.data;
					console.log(data.data)
				}
			},
			goRecord() {
				uni.navigateTo({
					url: '/subPage_receiptOrder/index'
				})
			},
			scanCode(code) {
				// if (this.dialog) {
				// 	return
				// }
				// this.queryCode = code;
				// this.getData()
			},
			openPic(index, type) {
				uni.previewImage({
					urls: type === 1 ? this.imagelist : this.historyImageList,
					current: index
				});
			},
			delImage(index) {
				this.imagelist.splice(index, 1)
			},
			async uploadByImage() {
				let _this = this;
				uni.chooseImage({
					//count: 9 - _this.imagelist.length, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera'], //从相册选择
					success: async (res) => {
						let paths = [];
						for (const [index, item] of res.tempFilePaths.entries()) {
							const url = await _this.$uploadFn(item, 0, 'receipt', 0, true);
							paths.push({
								index,
								url
							});
						}
						paths.sort((a, b) => a.index - b.index);
						for (const obj of paths) {
							_this.imagelist.push(obj.url);
						}
					}
				})
			},
			async submit() {
				if (this.disabled) return
				const items = [];
				let msg = '';
				this.form.productList.forEach(item => {
					if (item.confirmQuantity || item.confirmQuantity == '0') {
						if (!msg && parseInt(item.confirmQuantity) > parseInt(item.quantity)) {
							msg = `${item.productName}的实际交货件数不能大于客户订货件数`;
						}
						items.push({
							confirmQuantity: item.confirmQuantity,
							id: item.id
						})
					}
				})
				if (msg) {
					uni.showToast({
						title: msg,
						icon: 'none',
						duration: 2500
					})
					return
				}
				if (!items.length) {
					uni.showToast({
						title: '请填写至少一个货品的实际交货数量',
						icon: 'none',
						duration: 2000
					})
					return
				}

				if (!this.imagelist.length) {
					uni.showToast({
						title: '请上传回单拍照',
						icon: 'none',
						duration: 2000
					})
					return
				}
				let confirmImage = [];
				if (this.historyImageList.length) {
					confirmImage.push(...this.historyImageList);
				}
				confirmImage.push(...this.imagelist)
				const params = {
					confirmImage: confirmImage.join(),
					id: this.form.id,
					items: items
				}
				this.disabled = true;
				const data = await this.$api.post('/adm/supply/record/receipt', params)
				if (data.code === 200) {
					var pages = getCurrentPages();
					const preRoute = pages[pages.length - 2].route; //获取当前页面路由
					if (preRoute !== 'subPage_receiptOrder/index') {
						uni.redirectTo({
							url: '/subPage_receiptOrder/index'
						})
					} else {
						pages[pages.length - 2].$vm.downCallback()
						uni.navigateBack()
					}
				} else {
					this.disabled = false;
				}
			}
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.index {
		height: 100%;
		padding-bottom: 150upx;
		overflow: auto;

		.form-wrapper {
			background-color: #ffffff;
			margin: 20upx 20upx 0 20upx;
			border-radius: 20upx;
			padding: 25upx;

			.item-wrapper {
				display: flex;
				align-items: center;

				.label {
					width: 140upx;
				}

				&.marginTop {
					margin-top: 20upx;
				}
			}
		}

		.tip-wrapper {
			color: #ff8420;
		}

		.product-item {
			background-color: #ffffff;
			margin: 20upx 20upx 0 20upx;
			border-radius: 20upx;
			padding: 25upx;

			.product-name-wrapper {
				display: flex;
				align-items: center;

				.productName {
					flex: 1;
				}
			}

			.info-wrapper {
				margin-top: 20upx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.bold {
					font-weight: bold;
				}

				.input {
					flex: 1;
					font-weight: bold;
				}
			}
		}

		.images-wrapper {
			background-color: #ffffff;
			margin: 20upx 20upx 0 20upx;
			border-radius: 20upx;
			padding: 25upx;

			.title-wrapper {
				display: flex;
				align-items: center;

				.icon {
					margin-right: 5upx;
				}

				.title {
					height: 34upx;
					line-height: 34upx;
					font-size: 34upx;
					color: #222222;
					padding-left: 10upx;
					border-left: 8upx solid #555BFF;

					&.require::after {
						content: '*';
						color: red;
					}
				}
			}

			.product-image-list {
				display: flex;
				flex-wrap: wrap;
				margin-top: 26upx;

				.item {
					position: relative;
					width: 32%;
					height: 206upx;
					border-radius: 20upx;
					overflow: hidden;
					margin-left: 2%;
					margin-bottom: 20upx;

					&.add-item {
						display: flex;
						align-items: center;
						justify-content: center;
						background-color: #F0F0F0;
					}

					&:nth-child(3n+1) {
						margin-left: 0;
					}

					.product-image {
						width: 100%;
						height: 100%;
					}

					.del-wrapper {
						position: absolute;
						width: 50upx;
						height: 50upx;
						display: flex;
						align-items: center;
						justify-content: center;
						background-color: rgba(0, 0, 0, .5);
						top: 0;
						right: 0;
					}
				}
			}
		}

		.btn-wrapper {
			position: fixed;
			height: 100upx;
			width: 100%;
			bottom: 0;
			background-color: #ffffff;
			display: flex;
			align-items: center;
			z-index: 1;
			border-top: 1upx solid #efefef;

			.btn {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				height: 100%;
				background-color: #555BFF;
				color: #ffffff;

				&.default {
					color: #232323;
					background-color: #ffffff;
				}
			}
		}
	}
</style>
